package com.itcast.donkey.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.itcast.donkey.R;
import com.itcast.donkey.modles.ScenicAttackModles;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by lanouhn on 16/8/29.
 */
public class InAttackFrament extends Fragment{

    private ArrayList<ScenicAttackModles> modlesArrayList;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.view_activity_in_attack,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initData();
    }

    private void initData() {
        Intent intent = getActivity().getIntent();
        String title = intent.getStringExtra("result");
        //tvTitleScenicSpot.setText(title);
        final String uri = "http://chanyouji.com/api/wiki/destinations/55.json";
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(uri)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        handler.sendEmptyMessage(0);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        modlesArrayList = new ArrayList<>();
                        try {

                            JSONArray array = new JSONArray(result);
                            for (int i = 0; i < array.length(); i++) {

                                ScenicAttackModles notesModles = gson.fromJson(array.get(i).toString(), ScenicAttackModles.class);
                                modlesArrayList.add(notesModles);

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                        if (modlesArrayList.size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(2);
                        }
                    }
                });
            }
        }).start();
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "获取网络失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    break;
                case 2:
                    Toast.makeText(getActivity(), "解析失败", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
}
